package yf.platform.bo;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

public class JsonBizResult implements BizResult {
	private JSONObject obj = new JSONObject();
	private HttpServletRequest request;
	
	
	public JsonBizResult(HttpServletRequest request,int code, byte type, String msg) {
		super();
		this.request = request;
		obj.put("code", code);
		obj.put("type", type);
		obj.put("msg", msg);
	}

	public JsonBizResult(HttpServletRequest request,byte type, String page) {
		super();
		this.request = request;
		obj.put("type", type);
		obj.put("page", page);
	}

	public JsonBizResult(HttpServletRequest request,int code, String msg) {
		super();
		this.request = request;
		obj.put("code", code);
		obj.put("msg", msg);
	}

	public JsonBizResult(HttpServletRequest request,int code, Object data) {
		super();
		this.request = request;
		obj.put("code", code);
		obj.put("data", data);
	}
	
	

	public JsonBizResult(HttpServletRequest request) {
		super();
		this.request = request;
	}

	@Override
	public String getMsg() {
		return this.obj.getString("msg");
	}

	@Override
	public BizResult setMsg(String msg) {
		this.obj.put("msg", msg);
		return this;
	}

	@Override
	public int getCode() {
		return this.obj.getInt("code");
	}

	@Override
	public BizResult setCode(int code) {
		this.obj.put("code", code);
		return this;
	}

	@Override
	public String getPage() {
		return this.obj.getString("page");
	}

	@Override
	public BizResult setPage(String page) {
		this.obj.put("page", page);
		return this;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getData() {
		// TODO Auto-generated method stub
		return (T)this.obj.get("data");
	}

	@Override
	public <T> BizResult setData(T data) {
		this.obj.put("data", data);
		return this;
	}

	@Override
	public byte getType() {
		return (byte)this.obj.getInt("type");
	}

	@Override
	public BizResult setType(byte t) {
		this.obj.put("type", t);
		return this;
	}
	
	@Override
	public BizResult setReqAttribute(String k, Object v) {
		request.setAttribute(k, v);
		return this;
	}

	@Override
	public BizResult setSissAttribute(String k, Object v) {
		request.getSession().setAttribute(k, v);
		return this;
	}

	@Override
	public BizResult setAppAttribute(String k, Object v) {
		
		return this;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getReqAttribute(String k) {
		return (T)request.getAttribute(k);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getSissAttribute(String k) {
		return (T)request.getSession().getAttribute(k);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getAppAttribute(String k) {
		return (T)request.getSession().getServletContext().getAttribute(k);
	}

	@Override
	public BizResult removeReqAttribute(String k) {
		this.request.removeAttribute(k);
		return this;
	}

	@Override
	public BizResult removeSissAttribute(String k) {
		this.request.getSession().removeAttribute(k);
		return this;
	}
	
	@Override
	public String toString(){
		return this.obj.toString();
	}
	
}
